package com.ruoyi.common.utils.file;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

public class filedown {

    public void filedownexcel(List<?> list, HttpServletResponse response,String filename1,InputStream templatePathName) throws IOException {
        //第一步：申明需要用到的类
        ExcelWriter excelWriter = null;
        response.setCharacterEncoding("utf-8");
        String filename= URLEncoder.encode(filename1,"UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename="+filename + ".xlsx");
        response.setContentType("application/vnd.ms-excel;charset=gb2312");
        ServletOutputStream servletOutputStream=response.getOutputStream();
        EasyExcel.write(servletOutputStream).withTemplate(templatePathName).sheet().doFill(list);
    }



    public void filedownexcels(List<?> list, HttpServletResponse response, String filename1, InputStream templatePathName, Map<String,Object> map) throws IOException {
        //第一步：申明需要用到的类
        response.setCharacterEncoding("utf-8");
        String filename= URLEncoder.encode(filename1,"UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename="+filename + ".xlsx");
        response.setContentType("application/vnd.ms-excel;charset=gb2312");
        ServletOutputStream servletOutputStream=response.getOutputStream();
        try (ExcelWriter excelWriter = EasyExcel.write(servletOutputStream).withTemplate(templatePathName).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet().build();


            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            excelWriter.fill(list, fillConfig, writeSheet);
            excelWriter.fill(map, writeSheet);
        }

    }
}
